/*
 * Copyright (c) 2009-2010 HIT Microelectronic Center
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * Authors: Gou Pengfei
 *
 * Date: Dec. 2009
 *
 */

#include <algorithm>
#include <cstring>

#include "base/types.hh"
#include "base/trace.hh"
#include "config/the_isa.hh"
#include "cpu/edge/base_block.hh"
#include "sim/byteswap.hh"
#include "base/loader/symtab.hh"

template<class Impl>
BaseEdgeBlock<Impl>::BaseEdgeBlock(BlockID blockID, CPU * cpu)
    : cpu(cpu), blockID(blockID)
{
    initVars();
}

template<class Impl>
BaseEdgeBlock<Impl>::~BaseEdgeBlock()
{

#ifndef NDEBUG
    --cpu->blockcount;

    DPRINTF(EdgeBlockCount,
            "EdgeBlock: [Bid:%lli] destroyed. Blockcount for %s = %i\n",
            blockID, cpu->name(), cpu->blockcount);
#endif

}

template<class Impl>
void
BaseEdgeBlock<Impl>::initVars()
{
    flags.reset();

    typeFlags.reset();

    completeTypeFlags.reset();

    // Inst block is initialized as speculative.
    flags.set(IsSpeculative); 
    
    // Inst block is initialized as in correct path.
    flags.set(IsInCorrectPath);

    numWrites=0;
    numReads = 0;
    numStores =0;
    numLoads = 0;
    numUsefulInsts = 0;
    numMemRefs = 0;
    numControlInsts = 0;

    numConstInsts = 0;
    numTestInsts = 0;
    numReadWrites = 0;
    numDirectControls = 0;
    numIndirectControls = 0;
    numMoves = 0;
    numNullifies = 0;
    numPredOnTrues = 0;
    numPredOnFalses = 0;

    numExits = 0;

    tid = 0;

#ifndef NDEBUG
    ++cpu->blockcount;

    if (cpu->blockcount > 150) {
        assert(cpu->blockcount <= 150);
    }

    DPRINTF(EdgeBlockCount,
        "EdgeBlock: [Bid:%lli] created. Blockcount for %s = %i\n",
        blockID, cpu->name(), cpu->blockcount);
#endif
}

template<class Impl>
void
BaseEdgeBlock<Impl>::setStartPC(Addr pc)
{
    assert((pc&TheISA::ChunkOffset) == 0);
    startAddr = pc;
}

template<class Impl>
Addr
BaseEdgeBlock<Impl>::getStartPC()
{
    return startAddr;
}

